package com.shuwen.gcdj.common.sysLog;

import com.alibaba.fastjson.JSON;
import com.shuwen.gcdj.bean.member.request.LoginReq;
import com.shuwen.gcdj.bean.member.response.LoginResp;
import com.shuwen.gcdj.common.constant.Constants;
import com.shuwen.gcdj.common.exception.ApiException;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.entity.Log;
import com.shuwen.gcdj.mapper.mysql.LogMapper;
import com.shuwen.gcdj.service.BaseService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author :杨云山
 * 添加系统日志工具类
 * @date : 2023/3/15 10:48
 */
@Component
@AllArgsConstructor
@Slf4j
public class AddSysLogUtil extends BaseService<LogMapper, Log> {

    @Autowired
    private final LogMapper logMapper;

    /**
     *  添加系统日志  （常用）
     * type  操作类型  0 新增 1 修改 2 删除
     * module 模块 0 用户模块 1 字典模块
     * desc   日志描述（哪个用户做了什么)
     * params  请求参数
     * initUserDto  当前用户信息
     */
    public void addLog(InitUserDto initUserDto, Integer type, Integer module, String desc, Object params){
        if(StringUtils.isEmpty(initUserDto)){
            throw new ApiException("当前用户为空！");
        }
        if(StringUtils.isEmpty(initUserDto.getUserId())){
            throw new ApiException("当前用户ID为空！");
        }
        if(StringUtils.isEmpty(type)){
            throw new ApiException("type为空！");
        }
        if(StringUtils.isEmpty(module)){
            throw new ApiException("module为空！");
        }
        if(StringUtils.isEmpty(desc)){
            throw new ApiException("desc为空！");
        }
        if(StringUtils.isEmpty(params)){
            throw new ApiException("params为空！");
        }
        Log log = new Log();
        log.setDes("用户"+initUserDto.getLoginName()+" , "+desc);
        log.setModule(module);
        log.setCreateId(initUserDto.getUserId());
        log.setUpdateId(initUserDto.getUserId());
        log.setParams(JSON.toJSONString(params));
        log.setType(type);
        log.setIsDelete(Constants.IS_DELETE_NORMAL);
        logMapper.insert(log);
    }

    /**
     *  添加系统登录日志  （登录使用）
     * type  操作类型  0 新增 1 修改 2 删除
     * module 模块 0 用户模块 1 字典模块
     * desc   日志描述（哪个用户做了什么)
     * params  请求参数
     * LoginResp  当前用户信息
     */
    public void addLoginLog(LoginResp loginResp, Integer type, Integer module, String desc){
        if(StringUtils.isEmpty(loginResp)){
            throw new ApiException("当前用户为空！");
        }
        if(StringUtils.isEmpty(loginResp.getUserId())){
            throw new ApiException("当前用户ID为空！");
        }
        if(StringUtils.isEmpty(type)){
            throw new ApiException("type为空！");
        }
        if(StringUtils.isEmpty(module)){
            throw new ApiException("module为空！");
        }
        if(StringUtils.isEmpty(desc)){
            throw new ApiException("desc为空！");
        }
        LoginReq loginReq = new LoginReq();
        loginReq.setLoginName(loginResp.getLoginName());
        Log log = new Log();
        log.setDes("用户"+loginResp.getLoginName()+" , "+desc);
        log.setModule(module);
        log.setCreateId(loginResp.getUserId());
        log.setUpdateId(loginResp.getUserId());
        log.setParams(JSON.toJSONString(loginReq));
        log.setType(type);
        log.setIsDelete(Constants.IS_DELETE_NORMAL);
        logMapper.insert(log);
    }

}
